package com.kefan.reptilemanagement.service;

import com.kefan.reptilemanagement.common.entities.Spider;
import com.kefan.reptilemanagement.common.entities.Task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class Consumer {

    @Autowired
    TaskService taskService;
    @Autowired
    SpiderService spiderService;
    @RabbitListener(queues = "app.kefan.common")
    public void receiveConfirmMessage(Message message) throws InterruptedException {
        String msg = new String(message.getBody());
        String[]res=msg.split(",");
        Integer id=Integer.valueOf(res[0]);
        Integer time=Integer.valueOf(res[1]);
        System.out.println(id);
        System.out.println(time);
        Task task=taskService.getById(id);
        task.setStatus(2);
        taskService.updateById(task);
        Thread.sleep(time*1000);
        task.setStatus(3);
        task.setEndTime(LocalDateTime.now());
        Spider spider=spiderService.getById(task.getSpiderId());
        spider.setStatus(3);
        task.setResultFile(LocalDateTime.now().toString());
        spiderService.updateById(spider);
        taskService.updateById(task);

        log.info("接受到的队列confirm.queue消息：{}",msg);
    }

    @RabbitListener(queues = "app.kefan.fast")
    public void receiveConfirmMessage1(Message message) throws InterruptedException {
        String msg = new String(message.getBody());
        String[]res=msg.split(",");
        Integer id=Integer.valueOf(res[0]);
        Integer time=Integer.valueOf(res[1]);
        System.out.println(id);
        System.out.println(time);
        Task task=taskService.getById(id);
        task.setStatus(2);
        taskService.updateById(task);
        Thread.sleep(time*1000);
        task.setStatus(3);
        task.setEndTime(LocalDateTime.now());
        Spider spider=spiderService.getById(task.getSpiderId());
        spider.setStatus(3);
        spiderService.updateById(spider);
        task.setResultFile(LocalDateTime.now().toString());
        taskService.updateById(task);

        log.info("接受到的队列confirm.queue fast消息：{}",msg);
    }

    @RabbitListener(queues = "app.kefan.fast")
    public void receiveConfirmMessage2(Message message) throws InterruptedException {
        String msg = new String(message.getBody());
        String[]res=msg.split(",");
        Integer id=Integer.valueOf(res[0]);
        Integer time=Integer.valueOf(res[1]);
        System.out.println(id);
        System.out.println(time);
        Task task=taskService.getById(id);
        task.setStatus(2);
        taskService.updateById(task);
        Thread.sleep(time*1000);
        task.setStatus(3);
        task.setEndTime(LocalDateTime.now());
        Spider spider=spiderService.getById(task.getSpiderId());
        spider.setStatus(3);
        spiderService.updateById(spider);
        task.setResultFile(LocalDateTime.now().toString());
        taskService.updateById(task);

        log.info("接受到的队列confirm.queue fast消息：{}",msg);
    }
//    @RabbitListener(queues = "app.class.common2")
//    public void receive(Message message){
//        String msg = new String(message.getBody());
//        log.info("接受到的全局消息2：{}",msg);
//    }
//
//    @RabbitListener(queues = "app.class.common")
//    public void receivee(Message message){
//        String msg = new String(message.getBody());
//        log.info("接受到的全局消息1：{}",msg);
//    }
//
//
//    @RabbitListener(queues = "app.user.7")
//    public void recei(Message message){
//        String msg = new String(message.getBody());
//        log.info("user7接受到消息：{}",msg);
//    }
//
//    @RabbitListener(queues = "app.user.9")
//    public void re(Message message){
//        String msg = new String(message.getBody());
//        log.info("user9接受到的消息：{}",msg);
//    }

}
